<?php

namespace App\Http\Controllers\Admin\Upload;

use App\Exceptions\BasicException;
use App\Services\Tools\ObsService;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;    //引用http请求类
use App\Response\Response;

class ImageController extends BaseController {

    /**
     * @api {post} /upload/image/add 上传图片
     * @apiVersion 0.1.0
     * @apiName 上传图片
     * @apiGroup 上传
     *
     * @apiParam {file} file    礼物图片，必传

     * @apiSuccessExample 成功响应
     *
     *
    {
        "code": 200,
        "data": [
            {
            "city_id(城市id)": 110000,
            "name(名称)": "北京"
            }
        ]
    }
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 93020,
     *          "message": "errorMsg"
     *      }
     *
     */

    public  function add(Request $request){
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'file' => 'required',
            ]
        );

        if ($v->fails()) throw new BasicException(10001);

        $picture = $request->file('file');

        $res = ObsService::uploadFile($picture,['image/png','image/jpeg','image/jpg', "image/webp","image/gif","video/mp4", 'application/octet-stream', 'audio/mpeg']);

        return Response::sendData([
            "url" => $res,
        ]);
    }

    /**
     * Notes:api: /upload/image/file  多类型文件上传，冻结用户上传的凭证
     * User: wangyueli
     *
     * @param  Request  $request
     * @return string
     * @throws BasicException
     */
    public function otherUpload(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'file' => 'required',
            ]
        );

        if ($v->fails()) {
            throw new BasicException(10001);
        }

        $picture = $request->file('file');

        $res = ObsService::uploadFile($picture, [
            'application/pdf', 'application/pdf', 'image/png', 'image/jpeg', 'image/jpg', "image/webp", "image/gif", "video/mp4", 'application/octet-stream', 'audio/mpeg',
            'text/plain',
            'application/pdf',
            'application/msword',
            'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
            'application/vnd.ms-excel',
            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',

        ]);

        return Response::sendData([
            "url" => $res,
        ]);
    }
}
